﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TestEventBus
{
    class Program
    {
        static Thread workingThread;
        static EventWaitHandle waitHandle;

        static void Main(string[] args)
        {
            CPPEI.EventBus.EventBus.Get("abc").Register(new Receiver1());

            waitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
            workingThread = new Thread(Running);

            workingThread.Start();

            waitHandle.WaitOne();

            Console.WriteLine("press any key exit...");
            Console.ReadKey();
        }

        static void Running()
        {
            Console.WriteLine("-----async publish-----");
            for (int index = 0; index < 5; index++)
            {
                Thread.Sleep(1000);

                Console.WriteLine(string.Format("begin async publish:{0}", index));
                CPPEI.EventBus.EventBus.Get("abc").PublishAsync(new Event1(index));
                Console.WriteLine(string.Format("end async publish:{0}", index));
            }

            Console.WriteLine("-----sync publish-----");
            for (int index = 0; index < 5; index++)
            {
                Thread.Sleep(1000);

                Console.WriteLine(string.Format("begin publish:{0}", index));
                CPPEI.EventBus.EventBus.Get("abc").Publish(new Event1(index));
                Console.WriteLine(string.Format("end publish:{0}", index));
            }

            waitHandle.Set();
        }
    }

    class Receiver1
    {
        [CPPEI.EventBus.Subscribe]
        public void OnReceiveEvent1(Event1 @event)
        {
            Thread.Sleep(1000);
            Console.WriteLine(string.Format("data:{0}", @event.Data));

            if (@event.Data > 2)
            {
                throw new Exception("abc");
            }
        }
    }

    class TestEventBus1
    {
        public static string Name = "TEST_EVENTBUS";

    }

    class Event1
    {
        public int Data { get; set; }

        public Event1(int data)
        {
            Data = data;
        }
    }
}
